﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using ScrumTable.UI.ViewModel.Controller;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Split control with an header and body section
    /// </summary>
    public partial class SplitControl : ScrumUserControl
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets or sets the header control
        /// </summary>
        /// <value>The title control</value>
        public ScrumUserControl ControlHeader { get; set; }
        /// <summary>
        /// Gets or sets the body section
        /// </summary>
        /// <value>all controls placed in the body.</value>
        public IDictionary<NavigationTarget, ScrumUserControl> ControlBody { get; set; }
        #endregion


        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="SplitControl"/> class.
        /// </summary>
        public SplitControl()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="SplitControl"/> class.
        /// </summary>
        /// <param name="header">The menu control.</param>
        /// <param name="body">List of controls with target associated control. This controls are stored in the body section.</param>
        public SplitControl(ScrumUserControl header, IDictionary<NavigationTarget, ScrumUserControl> body)
        {
            ControlHeader = header;
            ControlBody = body;
            InitializeComponent();

            Header.Children.Add(ControlHeader);
            foreach (var pair in body)
            {
                TabView.Items.Add(new TabItem()
                   {
                       Name = Enum.GetName(typeof(NavigationTarget), pair.Key),
                       Content = pair.Value
                   });

            }
        }
        #endregion
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            
            foreach (var controls in ControlBody.Values)
            {
               controls.Dispose();   
            }
            ControlHeader.Dispose();
            ControlBody.Clear();
            TabView.Items.Clear();

            base.ReleaseManagedResources();
            
        }

        /// <summary>
        /// Determines whether the NavigationTarget is current a control within the body
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns>
        /// 	<c>true</c> if [is target in context] [the specified target]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsTargetInContext(NavigationTarget target)
        {
            string name = Enum.GetName(typeof(NavigationTarget), target);
            foreach (TabItem item in TabView.Items)
            {
                if (item.Name == name)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Shows NavigationTarget Control in the body
        /// </summary>
        /// <param name="target">NavigationTarget</param>
        public void OpenTarget(NavigationTarget target)
        {            
            string name = Enum.GetName(typeof(NavigationTarget), target);
            foreach (TabItem item in TabView.Items)
            {
                if (item.Name == name)
                {   
                    item.IsSelected = true;
                }
            }
        }
        #endregion
    }
}
